package com.lap.framework.junit.application;

import com.lap.framework.common.tool.JsonUtil;
import com.lap.framework.junit.application.builder.DefaultBuilder;
import com.lap.framework.junit.configure.ConfigLoader;
import com.lap.framework.junit.configure.EnvConfigure;
import com.lap.framework.junit.domain.ClassInfo;
import com.lap.framework.junit.domain.FileInfo;
import com.lap.framework.junit.domain.generator.CodeGeneratorFactory;
import com.lap.framework.junit.domain.repository.FileWriter;
import com.lap.framework.junit.infra.FileWriteImpl;
import com.lap.framework.junit.shared.tool.CodeType;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FileAppService {

  public List<FileInfo> execute() {
    EnvConfigure envConfigure = ConfigLoader.load();
    // 使用默认构造
    DefaultBuilder builder = new DefaultBuilder();
    builder.builder(envConfigure);
    List<ClassInfo> classes = builder.getClasses();

    List<FileInfo> all = new LinkedList<>();
    classes.stream()
        .filter(Objects::nonNull)
        .forEach(
            item -> {
              List<FileInfo> javaList =
                  CodeGeneratorFactory.getGenerator(CodeType.JAVA).generate(item);
              List<FileInfo> cvsList =
                  CodeGeneratorFactory.getGenerator(CodeType.CVS).generate(item);
              all.addAll(javaList);
              all.addAll(cvsList);
            });

    log.info("{}", JsonUtil.pretty(all));

    FileWriter writer = new FileWriteImpl();
    writer.write(all);
    return all;
  }
}
